<template>
  <el-dialog
    :title="`${detail.machineId}  ${detail.machineName}`"
    v-model="visible"
    width="850px"
    @close="onClose"
    :close-on-click-modal="false"
  >
    <div class="title">
      <div>设备温度记录表</div>
      <div class="title-r">
        <div style="display: flex; align-items: center; margin-right: 20px">
          检测频率:
          <el-link
            @click="handleFrequency"
            style="font-size: 18px"
            type="primary"
            :underline="false"
            >{{ tempFrequency }}分/次</el-link
          >
        </div>
        <div @click="handleShow">
          <el-icon size="30" color="#409EFF"><TrendCharts /></el-icon>
        </div>
      </div>
    </div>
    <div class="main">
      <el-table v-loading="loading" size="default" index :data="dataList">
        <el-table-column label="序号" type="index" width="220px" :index="indexAdd" align="center" />
        <el-table-column label="温度(°C)" align="center" prop="temp" />
        <el-table-column label="发生时间" align="center" prop="createTime" />
      </el-table>
      <div style="margin-right: 10px">
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
      <!-- 趋势图 -->
      <chart ref="chartRef" />
      <!-- 检测频率 -->
      <frequency ref="frequencyRef" @refresh="getFrequency" />
    </div>
  </el-dialog>
</template>

<script setup>
import chart from './chart.vue'
import frequency from './frequency.vue'
import { getDeviceTempListApi, getFrequencyApi } from '@/api/device'

const chartRef = ref(null)
const frequencyRef = ref()

const total = ref(0)
const dataList = ref([])
const visible = ref(false)

const queryParams = reactive({
  machineId: '',
  pageNum: 1,
  pageSize: 10
})
const loading = ref(true)

// 表格编号递增
function indexAdd(index) {
  const { pageNum, pageSize } = queryParams
  return index + 1 + (pageNum - 1) * pageSize
}

/** 查询用户列表 */
function getList() {
  loading.value = true
  getDeviceTempListApi(queryParams)
    .then((res) => {
      loading.value = false
      dataList.value = res?.data?.rows || []
      total.value = res?.data?.total || 0
    })
    .catch(() => {
      loading.value = false
      dataList.value = []
      total.value = 0
    })
}

// 查询温度频率
const tempFrequency = ref(0)
function getFrequency() {
  getFrequencyApi({ machineId: queryParams.machineId }).then((res) => {
    tempFrequency.value = res?.data || 0
  })
}

function onClose() {
  queryParams.pageNum = 1
  queryParams.pageSize = 10
}

// 趋势图
function handleShow() {
  chartRef.value?.init(queryParams.machineId)
}

// 温度检测频率
function handleFrequency() {
  frequencyRef.value?.init(queryParams.machineId, tempFrequency.value)
}

const detail = ref({})
function init(row) {
  visible.value = true
  queryParams.machineId = row.machineId
  detail.value = row
  getList()
  getFrequency()
}

defineExpose({
  init
})
</script>

<style lang="scss" scoped>
:deep(.pagination-container .el-pagination) {
  right: 20px;
}
.title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 20px;
  margin-bottom: 20px;
}
.title-r {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 18px;
}
</style>
